//
//  BaseViewController.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "BaseViewController.h"


#define LAST_RUN_VERSION_KEY @"last_run_version_of_application"

@interface BaseViewController ()

@property (strong, nonatomic) UIView * activityBGView;
@property (strong, nonatomic) UIActivityIndicatorView * activityView;

@end

@implementation BaseViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor globalBackgroundColor];
    
    //添加导航条
    [self.view addSubview:self.baseNavView];
    
    [self addNotifications];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}


- (void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    
    self.baseNavView.width = KScreenWidth;
    self.baseNavView.height = TJ_NavBarHeight;
}


//导航条
- (BaseNavView *)baseNavView{
    if (!_baseNavView) {
        _baseNavView = [[BaseNavView alloc] initWithTitle:@""];
    }
    return _baseNavView;
}

#pragma mark - 判断是否是第一次安装

- (BOOL) isFirstLoad
{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]
                                objectForKey:@"CFBundleShortVersionString"];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *lastRunVersion = [defaults objectForKey:LAST_RUN_VERSION_KEY];
    
    if (!lastRunVersion) {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        return YES;
    }
    else if (![lastRunVersion isEqualToString:currentVersion]) {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        return YES;
    }
    return NO;
}



#pragma mark - UITableView.Section.分割线

- (UIView *)addTableViewSectionSegmentationLineByY:(CGFloat)y{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = mBackgroudColor;
    
    CAShapeLayer *line = [CAShapeLayer layer];
    line.fillColor = mIconLineColor.CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, y, KScreenWidth, 0.5)];
    line.path = path.CGPath;
    [view.layer addSublayer:line];
    
    return view;
}


#pragma mark - progressView (显示/隐藏 加载中)

- (void)showProgressView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self.activityBGView];
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = NO;
   // [self.activityView startAnimating];
}

- (void)hideProgressView
{
   // [self.activityView stopAnimating];
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = YES;
    [self.activityBGView removeFromSuperview];
}



#pragma mark - getter methods

- (UIView *)activityBGView
{
    if (!_activityBGView) {
        _activityBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        _activityBGView.clipsToBounds = YES;
        _activityBGView.backgroundColor = RGBA(0, 0, 0, 0.0);
        _activityBGView.userInteractionEnabled = NO;
        
        //黑色小背景
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        bgView.backgroundColor = RGBA(0, 0, 0, .4);
        bgView.layer.cornerRadius = 6;
        bgView.clipsToBounds = YES;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.size = CGSizeMake(bgView.width - 20, bgView.height -20);
        imageView.mj_x = 10;
        imageView.mj_y = 10;
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [bgView addSubview:imageView];
        
        
        NSMutableArray *imageArr = [NSMutableArray array];
        for (int i = 1; i < 7; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"meat_loading_0%d", i]];
            [imageArr addObject:image];
        }
        
        imageView.animationImages = imageArr;
        imageView.animationDuration = 0.8;
        imageView.animationRepeatCount = MAXFLOAT;
        [imageView startAnimating];

        [self.activityBGView addSubview:bgView];
        bgView.center = _activityBGView.center;
                
    }
    return _activityBGView;
}

- (UIActivityIndicatorView *)activityView
{
    if (!_activityView) {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    }
    return _activityView;
}


- (FMTagsView *)setupFMTagsView
{
    FMTagsView *tagsView = [[FMTagsView alloc] initWithFrame: CGRectMake(15, 0, KScreenWidth -30, 300)];
    
    tagsView.contentInsets = UIEdgeInsetsZero;
    tagsView.tagInsets = UIEdgeInsetsMake(5, 15, 5, 15);
    
    tagsView.tagFont = mFont(12);
    tagsView.tagSelectedFont = mFont(12);
    tagsView.tagBorderWidth = 1;
    tagsView.tagcornerRadius = 15;
    tagsView.lineSpacing = 5;
    tagsView.interitemSpacing = 5;
    tagsView.tagHeight = 27;
    tagsView.tagBorderColor = mBackgroudColor;
    tagsView.tagSelectedBorderColor = mBackgroudColor;
    tagsView.tagBackgroundColor = mBackgroudColor;
    tagsView.tagSelectedBackgroundColor = mBackgroudColor;
    tagsView.tagTextColor = UIColorFromRGB(0x6F6F6F);
    tagsView.tagSelectedTextColor = UIColorFromRGB(0x6F6F6F);
    
    return tagsView;
}


- (float)getFMTagViewHeight:(FMTagsView *)tagsView
{
    CGSize contentSize = tagsView.collectionView.collectionViewLayout.collectionViewContentSize;
    return contentSize.height;
}


#pragma mark - notifications

- (void)addNotifications
{
    
}

- (void)removeNotifications
{
    
}


#pragma mark -system

- (UIStatusBarStyle) preferredStatusBarStyle
{
    ///这里设置白色
    return UIStatusBarStyleLightContent;
}


- (BOOL)prefersStatusBarHidden
{
    return NO;
}


- (UIRectEdge)edgesForExtendedLayout
{
    return UIRectEdgeNone;
}

- (void)dealloc
{
    PMLog(@"%@ is dealloc",NSStringFromClass(self.class));
    [self removeNotifications];
}

@end
